#ifndef  __SMART_POINTER_H__
#define  __SMART_POINTER_H__

/*
 * 实现效过如下
 * # 指针生命周期结束时主动释放堆区空间.
 * # 一片堆空间最多只能由一个指针指向
 * # 不允许指针运算和指针比较
 */

namespace EEQLib {
    template <typename T> class SmartPointer {
    protected:
        T* m_pointer;
    public:
        SmartPointer( T* p = nullptr ) {
            this->m_pointer = p;
        }


        SmartPointer( const SmartPointer<T>& obj ) { /* 拷贝构造函数 */
            this->m_pointer = obj.m_pointer;
            const_cast<SmartPointer<T> &>(obj).m_pointer = nullptr;
        }
        

        SmartPointer<T>& operator=( const SmartPointer<T>& obj ) {
            
            if ( this != &obj ) {  /* 不允许自己给自己赋值 */
                delete m_pointer;
                this->m_pointer = obj.m_pointer;
                const_cast<SmartPointer<T>&>(obj).m_pointer = nullptr;
            }
    
            return *this;
        }


        T*  operator-> () {
            return this->m_pointer;
        }


        T&  operator* () {
            return this->m_pointer;
        }


        bool  isNull() {
            return (this->m_pointer == nullptr);
        }


        T*  get() {
            return this->m_pointer;
        }


        ~SmartPointer() {
            delete m_pointer;
        }
    };
}

#endif
